﻿/*
 *	os_mutex.h
 *
 *  Created on: 2017年4月1日
 *  Author: wangwei
 */


#include "xmutex.h"

CriticalSection::CriticalSection(void)
{
#ifndef WIN32
	pthread_mutex_init(&m_event_mutex, 0);
#else
	InitializeCriticalSection(&m_event_mutex);
#endif
}

CriticalSection::~CriticalSection(void)
{
#ifdef WIN32
	DeleteCriticalSection(&m_event_mutex);
#else
	pthread_mutex_destroy(&m_event_mutex);
#endif
}

void CriticalSection::enter_mutex()
{
#ifdef WIN32
	EnterCriticalSection(&m_event_mutex);
#else
	pthread_mutex_lock(&m_event_mutex);
#endif
}

void CriticalSection::leave_mutex()
{
#ifdef WIN32
	LeaveCriticalSection(&m_event_mutex);
#else
	pthread_mutex_unlock(&m_event_mutex);
#endif
}

CGuardMutex::CGuardMutex(CriticalSection &cirt)
	: m_stCS(cirt)
{
	cirt.enter_mutex();
}

CGuardMutex::~CGuardMutex()
{
	m_stCS.leave_mutex();
}
